﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

public partial class BaseData_ProductImg : System.Web.UI.Page, ICallbackEventHandler
{
    string result = "";
    //protected void Page_PreInit(object sender, EventArgs e)
    //{
    //    Page.Theme = BLL.Common.GetWSMThemes();
    //}
    public string spdm = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        //Check.CheckPermission("200200101");
        if (Request.QueryString["spdm"] == null)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener=null;window.close();", true);
        }
        else
        {
            spdm = Request.QueryString["spdm"].ToString().Trim();
        }
        if (!IsPostBack)
        {
            DataSet ds = new BLL.SHANGPIN().GetSPGG1(spdm);
            DropDownList1.DataSource = ds.Tables[0].DefaultView;
            DropDownList1.DataBind();
        }
        CheckBox1.Attributes.Add("onclick", "SetAddButton();");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00006"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (File1.Value.Trim() == "")
        {
            Share.Alert(Page, "请选择要上传的文件！"); return;
        }


        System.Drawing.Image m_Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream, true);
        string imgsizedir = "";
        string ggdm = DropDownList1.SelectedValue.Trim();
        System.Random rd = new Random();
        string rdstr = "";
        string imgname = "";

        /******  新增部分 ******/
        string filename;

        //string filePath = File1.PostedFile.FileName;
        //string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();
        //string type = File1.PostedFile.ContentType;
        /************/

        for (int a = 0; a < 4; a++)
        {
            rdstr += rd.Next(9).ToString();
        }

        if (CheckBox1.Checked)
        {
            if (new BLL.COMMODITY_PIC().CheckDefaultExists(spdm))
            {
                Share.Alert(Page, "该商品已有默认图片,并且只允许存在一个！"); return;
            }
        }
        if (!CheckBox2.Checked)
        {
            if (new BLL.COMMODITY_PIC().CheckColorPicExists(spdm, ggdm))
            {
                Share.Alert(Page, "该颜色已传图片,并且只允许存在一个！"); return;
            }
        }

        if (!CheckBox2.Checked)
        {

            if (HttpContext.Current.Request.Files.Count > 1)
            {
                Share.Alert(Page, "局部图片才可以多图片上传！");

                return;
            }
        }


        HttpFileCollection fff = HttpContext.Current.Request.Files;
        for(int kk=0;kk<fff.Count;kk++){
            if (fff[kk].FileName.ToString() != "") {
                if (fff[kk].ContentType == "image/bmp" || fff[kk].ContentType == "image/gif" || fff[kk].ContentType == "image/pjpeg")
                {
                    continue;
                }
                else {
                    Share.Alert(Page, "只能上传图片格式的文件！");
                    return;
                }
            }
        }


        #region 上传图片
        Model.SYSPARAM syspa = new BLL.SYSPARAM().GetModel("001");
        List<string> guige = new List<string>();
        guige.Add(syspa.SP_PARAM16);
        guige.Add(syspa.SP_PARAM17);
        guige.Add(syspa.SP_PARAM18);
        guige.Add(syspa.SP_PARAM19);
        if (syspa.SP_PARAM1 == 1)
        {//ftp
            string ftpip = syspa.SP_PARAM13;
            string ftpuser = syspa.SP_PARAM14;
            string ftppwd = syspa.SP_PARAM15;
            FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
            bool result = false;
            for (int i = 0; i < guige.Count; i++)
            {
                filename = DateTime.Now.Ticks.ToString();

                Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + filename + ".jpg"), Int32.Parse(guige[i].Split(',')[0]), Int32.Parse(guige[i].Split(',')[1]));
                // 把上传的文件写入磁盘

                switch (i)
                {
                    case 0:
                        imgsizedir = "small";
                        break;
                    case 1:
                        imgsizedir = "com";
                        break;
                    case 2:
                        imgsizedir = "big";
                        break;
                    case 3:
                        imgsizedir = "show";
                        break;
                }
                result = false;
                if (!fud.IsExistsDirectory("", "ProductImg", out result))
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                    if (fud.MakeDir("", "ProductImg"))
                    {
                        Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                    }
                }
                else
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }
                result = false;
                if (!fud.IsExistsDirectory("ProductImg/", imgsizedir, out result))
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                    if (fud.MakeDir("ProductImg/", imgsizedir))
                    {
                        Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                    }
                }
                else
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }
                result = false;
                if (!fud.IsExistsDirectory("ProductImg/" + imgsizedir + "/", spdm, out result))
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                    if (fud.MakeDir("ProductImg/" + imgsizedir + "/", spdm))
                    {
                        Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                    }
                }
                else
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }
                result = false;
                if (!fud.IsExistsDirectory("ProductImg/" + imgsizedir + "/" + spdm + "/", ggdm, out result))
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                    if (fud.MakeDir("ProductImg/" + imgsizedir + "/" + spdm + "/", ggdm))
                    {
                        Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                    }
                }
                else
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }
                imgname = spdm + "_" + ggdm + "_" + rdstr + ".jpg";
                fud.Upload("ProductImg/" + imgsizedir + "/" + spdm + "/" + ggdm + "/", HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + filename + ".jpg"), imgname);
                if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + filename + ".jpg")))
                {
                    File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + filename + ".jpg"));
                }
            }
        }
        else
        {
            for (int c = 0; c < guige.Count; c++)
            {
                switch (c)
                {
                    case 0:
                        imgsizedir = "small";
                        break;
                    case 1:
                        imgsizedir = "com";
                        break;
                    case 2:
                        imgsizedir = "big";
                        break;
                    case 3:
                        imgsizedir = "show";
                        break;
                }
                imgname = spdm + "_" + ggdm + "_" + rdstr + ".jpg";
                Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImg/" + imgsizedir + "/" + spdm + "/" + ggdm + "/" + imgname), Int32.Parse(guige[c].Split(',')[0]), Int32.Parse(guige[c].Split(',')[1]));
            }
        }
        #endregion



        Model.COMMODITY_PIC CP = new Model.COMMODITY_PIC();
        CP.CP_CODE = spdm;
        CP.CP_COLOR = ggdm;
        CP.CP_PICPATH = imgname;
        CP.CP_PART = CheckBox2.Checked == true ? 1 : 0;
        CP.CP_DEFAULT = CheckBox1.Checked == true ? 1 : 0;
        if (new BLL.COMMODITY_PIC().Add(CP) != null)
        {
            //  Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.href='ProductList.aspx?key="+Request.QueryString["spdm"].ToString()+"&sxpic=tt&type=0';window.close();", true);
        }
        else
        {
            Share.Alert(this.Page, "上传失败");
        }





        //局部图片



        #region 局部图片多上传
        if (CheckBox2.Checked)
        {


            HttpFileCollection files = HttpContext.Current.Request.Files;


            for (int xx = 1; xx < files.Count; xx++)
            {




              


                if (files[xx].FileName.ToString() == "")
                {
                    continue;
                }






                System.Drawing.Image jbt_m_Image = System.Drawing.Image.FromStream(files[xx].InputStream, true);
                string jbt_imgsizedir = "";
                string jbt_ggdm = DropDownList1.SelectedValue.Trim();
                System.Random jbt_rd = new Random();
                string jbt_rdstr = "";
                string jbt_imgname = "";

                /******  新增部分 ******/
                string jbt_filename;

                //string filePath = File1.PostedFile.FileName;
                //string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();
                //string type = File1.PostedFile.ContentType;
                /************/

                for (int a = 0; a < 4; a++)
                {
                    jbt_rdstr += jbt_rd.Next(9).ToString();
                }
                 

                #region 上传图片
                Model.SYSPARAM jbt_syspa = new BLL.SYSPARAM().GetModel("001");
                List<string> jbt_guige = new List<string>();
                jbt_guige.Add(jbt_syspa.SP_PARAM16);
                jbt_guige.Add(jbt_syspa.SP_PARAM17);
                jbt_guige.Add(jbt_syspa.SP_PARAM18);
                jbt_guige.Add(jbt_syspa.SP_PARAM19);
                if (jbt_syspa.SP_PARAM1 == 1)
                {//ftp
                    string ftpip = jbt_syspa.SP_PARAM13;
                    string ftpuser = jbt_syspa.SP_PARAM14;
                    string ftppwd = jbt_syspa.SP_PARAM15;
                    FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
                    bool result = false;
                    for (int i = 0; i < jbt_guige.Count; i++)
                    {
                        jbt_filename = DateTime.Now.Ticks.ToString();

                        Share.Convert(jbt_m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + jbt_filename + ".jpg"), Int32.Parse(jbt_guige[i].Split(',')[0]), Int32.Parse(jbt_guige[i].Split(',')[1]));
                        // 把上传的文件写入磁盘

                        switch (i)
                        {
                            case 0:
                                jbt_imgsizedir = "small";
                                break;
                            case 1:
                                jbt_imgsizedir = "com";
                                break;
                            case 2:
                                jbt_imgsizedir = "big";
                                break;
                            case 3:
                                jbt_imgsizedir = "show";
                                break;
                        }
                        result = false;
                        if (!fud.IsExistsDirectory("", "ProductImg", out result))
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                            if (fud.MakeDir("", "ProductImg"))
                            {
                                Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                            }
                        }
                        else
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                        }
                        result = false;
                        if (!fud.IsExistsDirectory("ProductImg/", jbt_imgsizedir, out result))
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                            if (fud.MakeDir("ProductImg/", jbt_imgsizedir))
                            {
                                Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                            }
                        }
                        else
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                        }
                        result = false;
                        if (!fud.IsExistsDirectory("ProductImg/" + jbt_imgsizedir + "/", spdm, out result))
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                            if (fud.MakeDir("ProductImg/" + jbt_imgsizedir + "/", spdm))
                            {
                                Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                            }
                        }
                        else
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                        }
                        result = false;
                        if (!fud.IsExistsDirectory("ProductImg/" + jbt_imgsizedir + "/" + spdm + "/", jbt_ggdm, out result))
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                            if (fud.MakeDir("ProductImg/" + jbt_imgsizedir + "/" + spdm + "/", jbt_ggdm))
                            {
                                Share.Alert(this.Page, "创建目录失败,请重试!"); return;
                            }
                        }
                        else
                        {
                            if (result == false)
                            {
                                Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                            }
                        }
                        jbt_imgname = spdm + "_" + jbt_ggdm + "_" + jbt_rdstr + ".jpg";
                        fud.Upload("ProductImg/" + jbt_imgsizedir + "/" + spdm + "/" + jbt_ggdm + "/", HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + jbt_filename + ".jpg"), jbt_imgname);
                        if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + jbt_filename + ".jpg")))
                        {
                            File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImageUpload/" + jbt_filename + ".jpg"));
                        }
                    }
                }
                else
                {
                    for (int c = 0; c < jbt_guige.Count; c++)
                    {
                        switch (c)
                        {
                            case 0:
                                jbt_imgsizedir = "small";
                                break;
                            case 1:
                                jbt_imgsizedir = "com";
                                break;
                            case 2:
                                jbt_imgsizedir = "big";
                                break;
                            case 3:
                                jbt_imgsizedir = "show";
                                break;
                        }
                        jbt_imgname = spdm + "_" + jbt_ggdm + "_" + jbt_rdstr + ".jpg";
                        Share.Convert(jbt_m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ProductImg/" + jbt_imgsizedir + "/" + spdm + "/" + jbt_ggdm + "/" + jbt_imgname), Int32.Parse(jbt_guige[c].Split(',')[0]), Int32.Parse(jbt_guige[c].Split(',')[1]));
                    }
                }
                #endregion 






                        Model.COMMODITY_PIC jbt_CP = new Model.COMMODITY_PIC();
                        jbt_CP.CP_CODE = spdm;
                        jbt_CP.CP_COLOR = jbt_ggdm;
                        jbt_CP.CP_PICPATH = jbt_imgname;
                        jbt_CP.CP_PART = CheckBox2.Checked == true ? 1 : 0;
                        jbt_CP.CP_DEFAULT = CheckBox1.Checked == true ? 1 : 0;
                        new BLL.COMMODITY_PIC().Add(jbt_CP);
 




            }
        }


        #endregion






        Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.href='ProductList.aspx?key=" + Request.QueryString["spdm"].ToString() + "&sxpic=tt&type=0';window.close();", true);
    







    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        if (new BLL.COMMODITY_PIC().CheckDefaultExists(spdm, eventArgument))
        {
            result = "1";
        }
        else
        {
            result = "0";
        }
    }
    public string GetCallbackResult()
    {
        return result;
    }
}
